﻿using BaseUtils.Helper;
using Newtonsoft.Json;
using NWMES.DataCollectForResourceFAIServiceService;
using NWMES.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using machineIntegrationParametricData = NWMES.DataCollectForResourceFAIServiceService.machineIntegrationParametricData;

namespace NWMES.Mes
{

    /// <summary>
    /// 首件接口
    /// </summary>
    public class DataCollectForResourceFAIService : MESBaseService
    {
        static string DefaultApiSection = "DataCollectForResourceFAIServiceService";
        public static NetworkCredential networkCredential = null;
        public static DataCollectForResourceFAIService Params = null;

        static DataCollectForResourceFAIService()
        {
            Params = config[DefaultApiSection].ToObject<DataCollectForResourceFAIService>();
            networkCredential = new NetworkCredential(Params.UserName, Params.Password);
        }

        /// <summary>
        /// 首件(带收数)接口
        /// </summary>
        /// <param name="sfc"></param>
        /// <param name="MesData"></param>
        /// <returns></returns>
        public static MesResult RequestMes(string sfc, List<MachineIntegrationParamData> MesData,
            string ApiSection)
        {
            DataCollectForResourceFAIServiceService.DataCollectForResourceFAIServiceService collectFAIService;
            dataCollectForResourceFAIRequest Request;

            if (!string.IsNullOrEmpty(ApiSection))
            {
                Params = config[ApiSection].ToObject<DataCollectForResourceFAIService>();
                networkCredential = new NetworkCredential(Params.UserName, Params.Password);
            }
            else
            {
                ApiSection = DefaultApiSection;
            }
            string ErrorMessage = Params.ParameterVerification();
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                return new MesResult(-98, ErrorMessage);
            }
            SetRequestParams(out collectFAIService, out Request);

            //传入sfc
            Request.sfc = sfc;

            if (MesData?.Count > 0)
            {
                Request.parametricDataArray = MesData.Select(a =>
                {
                    machineIntegrationParametricData param = new machineIntegrationParametricData()
                    {
                        name = a.Name,
                        value = a.Value,
                        dataType = a.DataType.ToString().ToEnum<ParameterDataType>()
                    };
                    return param;
                }).ToArray();
            }

            dataCollectForResourceFAIResponse Response = new dataCollectForResourceFAIResponse();

            MesResult result = MesResult.InitMesResult(JsonConvert.SerializeObject(Request));
            dataCollectForResourceFAI DataCollectForSfc = new dataCollectForResourceFAI();
            DataCollectForSfc.resourceRequest = Request;

            try
            {
                Response = collectFAIService.dataCollectForResourceFAI(DataCollectForSfc);

                result.EndTime = DateTime.Now;
                var ret = Response.@return;
                result.Message = ret?.message;
                result.Code = ret.code;
                result.Success = result.Code == 0;
                result.ResponeJson = JsonConvert.SerializeObject(Response);
                result.Respone = Response;
                result.ret = ret;
            }
            catch (Exception ex)
            {
                result.Code = -100;
                result.Message = ex.Message + ";堆栈信息：" + ex.StackTrace;
            }
            SaveMesLog(ApiSection, result);
            return result;
        }


        public static MesResult RequestMes(string sfc, List<KeyValuePair<string, string>> MesData)
        {
            DataCollectForResourceFAIServiceService.DataCollectForResourceFAIServiceService collectFAIService;
            dataCollectForResourceFAIRequest Request;

            SetRequestParams(out collectFAIService, out Request);

            //传入sfc
            Request.sfc = sfc;

            if (MesData?.Count > 0)
            {

                Request.parametricDataArray = MesData.Select(a =>
                {
                    machineIntegrationParametricData param = new machineIntegrationParametricData()
                    {
                        name = a.Key,
                        value = a.Value,
                        dataType = a.Key == "CHANNEL" ? DataCollectForResourceFAIServiceService.ParameterDataType.TEXT : DataCollectForResourceFAIServiceService.ParameterDataType.NUMBER
                    };
                    return param;
                }).ToArray();
            }

            dataCollectForResourceFAIResponse Response = new dataCollectForResourceFAIResponse();

            MesResult result = MesResult.InitMesResult(JsonConvert.SerializeObject(Request));
            dataCollectForResourceFAI DataCollectForSfc = new dataCollectForResourceFAI();
            DataCollectForSfc.resourceRequest = Request;

            try
            {
                Response = collectFAIService.dataCollectForResourceFAI(DataCollectForSfc);

                result.EndTime = DateTime.Now;
                var ret = Response.@return;
                result.Message = ret?.message;
                result.Code = ret.code;
                result.Success = result.Code == 0;
                result.ResponeJson = JsonConvert.SerializeObject(Response);
            }
            catch (Exception ex)
            {
                result.Code = -99;
                result.Message = ex.Message;
            }

            SaveMesLog(DefaultApiSection, result);
            return result;
        }

        private static void SetRequestParams(out DataCollectForResourceFAIServiceService.DataCollectForResourceFAIServiceService machineIntegrationServiceService, out dataCollectForResourceFAIRequest DcForModuleTestRequest)
        {
            machineIntegrationServiceService = new DataCollectForResourceFAIServiceService.DataCollectForResourceFAIServiceService();
            machineIntegrationServiceService.Url = Params.Url;
            machineIntegrationServiceService.Timeout = Params.Timeout;
            machineIntegrationServiceService.Credentials = networkCredential;
            machineIntegrationServiceService.PreAuthenticate = true;
            DcForModuleTestRequest = new dataCollectForResourceFAIRequest();
            DcForModuleTestRequest.site = Params.Site;
            DcForModuleTestRequest.user = Params.User;
            DcForModuleTestRequest.operation = Params.Operation;
            DcForModuleTestRequest.operationRevision = Params.OperationRevision;
            //DcForModuleTestRequest.activityId = Params.ActivityId;
            DcForModuleTestRequest.resource = Params.Resource;
            DcForModuleTestRequest.dcMode = Params.ModeProcessSFC;
            DcForModuleTestRequest.dcGroupSequence = Params.dcGroupSequence;

            DcForModuleTestRequest.dcGroup = Params.DcGroup;
            DcForModuleTestRequest.dcGroupRevision = Params.DcGroupRevision;

        }
        /// <summary>
        /// DC参数数组参数名称
        /// </summary>
        public string name { get; private set; } = "";

        /// <summary>
        /// NC参数数组NCCODE
        /// </summary>
        public string ncCode { get; private set; } = "";

        /// <summary>
        /// NC参数数组布尔值
        /// </summary>
        public string hasNc { get; private set; } = "";
    }
}
